import React, { Component } from 'react'
import { getlogin } from '../../api/index'

export default class componentName extends Component {
  state = {
    username: '',
    password: ''
  }

  componentWillMount() {
    this.props.history.replace('/login')
  }

  render() {
    const { username, password } = this.state;
    return (
      <div>
        <input value={username} onChange={this.handleUser} placeholder="请输入账号" />
        <input value={password} onChange={this.handlePwd} placeholder="请输入密码" />
        <button onClick={this.handleLogin}>提交</button>
      </div>
    )
  }

  handleUser = (e) => {
    const val = e.target.value;
    this.setState({
      username: val
    })
  }

  handlePwd = (e) => {
    const val = e.target.value;
    this.setState({
      password: val
    })
  }

  handleLogin = () => {
    if (this.state.username && this.state.password) {
      getlogin({
        username: this.state.username,
        password: this.state.password
      }).then(res => {
        console.log(res, 'tiken')
        if (res.token) {
          sessionStorage.setItem('token', res.token)
          this.props.history.push('/homeindex/book')
        } else {
          alert(res.msg)
        }
      })
    } else {
      alert('输入密码')
    }

  }
}
